<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-30
 * Time: 14:18
 */

namespace app\merchant\controller;


use app\merchant\define\Merchant;
use app\merchant\service\MerPay;
use think\Log;
use think\Request;

class Archives extends Basic{
    protected $archives_service = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->archives_service = new \app\merchant\service\Archives();
    }

    public function _initialize()
    {
        parent::_initialize();
        $arr[0]['text']   = '列表';
        $arr[0]['url']    = '';
        $this->assign('third_menu', $arr);

    }

    public function intentlist(){
        $where = input('');
        $ret = $this->archives_service->getList("intent",$this->merchant['id'],$where);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("records");
        $this->assign("records",$records);
        $this->assign("where",$where);
        return $this->fetch();
    }

    public function intentadd() {
        if($this->request->isPost()){
            $_form = input('');
            $_form['mer_id'] = $this->merchant['id'];
            $ret = $this->archives_service->add("intent",$_form);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            return $this->fetch();
        }
    }

    public function intentedit(){
        if($this->request->isPost()) {
            $_form = input('');
            $ret = $this->archives_service->edit("intent",$_form,$_form['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            $id = input('id');
            $ret = $this->archives_service->getOne("intent",$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            $one = $ret->getData("one");
            $this->assign("one",$one);
            return $this->fetch();
        }
    }

    public function intentdel() {
        $id = input('id');
        $ret = $this->archives_service->del("intent",$id);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
        }
        return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
    }

    public function arealist() {
        $where = input('');
        $ret = $this->archives_service->getAreaList($this->merchant['id'],isset($where['name'])?$where['name']:null);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("records");
        $this->assign("records",$records);
        $this->assign("where",$where);
        return $this->fetch();
    }

    public function areaadd() {
        if($this->request->isPost()){
            $_form = input('');
            $_form['mer_id'] = $this->merchant['id'];
            $ret = $this->archives_service->add("area",$_form);
            if($ret->getCode() == Merchant::$Code_Mer_Error){
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }else {
                return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
            }
        }else {
            $ret = $this->archives_service->getAreas($this->merchant['id'],0);
            $areas = $ret->getData("records");
            $this->assign("areas",$areas);
            return $this->fetch();
        }
    }

    public function areaedit() {
        if($this->request->isPost()) {
            $_form = input('');
            $ret = $this->archives_service->edit("area",$_form,$_form['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            $id = input('id');
            $ret = $this->archives_service->getOne("area",$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                $this->error($ret->getMsg());
            }
            $area = $ret->getData("one");
            $this->assign("one",$area);
            $ret = $this->archives_service->getAreas($this->merchant['id'],$area['id']);
            $areas = $ret->getData("records");
            $this->assign("areas",$areas);
            return $this->fetch();
        }
    }

    public function areadel() {
        $id = input('id');
        $ret = $this->archives_service->del("area",$id);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
        }
        return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
    }

    public function scenelist() {
        $where = input('');
        $ret = $this->archives_service->getList("scene",$this->merchant['id'],$where);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("records");
        $this->assign("records",$records);
        $this->assign("where",$where);
        return $this->fetch();
    }

    public function sceneadd(){
        if($this->request->isPost()) {
            $_form = input('');
            $_form['mer_id'] = $this->merchant['id'];
            $ret = $this->archives_service->add("scene",$_form);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            return $this->fetch();
        }
    }

    public function sceneedit() {
        if($this->request->isPost()) {
            $_form = input('');
            $ret = $this->archives_service->edit("scene",$_form,$_form['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            $id = input('id');
            $ret = $this->archives_service->getOne("scene",$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                $this->error($ret->getMsg());
            }
            $scene = $ret->getData("one");
            $this->assign("one",$scene);
            return $this->fetch();
        }
    }

    public function scenedel() {
        $id = input('id');
        $ret = $this->archives_service->del("scene",$id);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
        }
        return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
    }

    public function seasonlist() {
        $where = input('');
        $ret = $this->archives_service->getList("season",$this->merchant['id'],$where);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("records");
        $this->assign("records",$records);
        $this->assign("where",$where);
        return $this->fetch();
    }

    public function seasonadd() {
        if($this->request->isPost()) {
            $_form = input('');
            $_form['mer_id'] = $this->merchant['id'];
            $ret = $this->archives_service->add("season", $_form,$this->merchant['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error){
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            return $this->fetch();
        }
    }

    public function seasonedit() {
        if($this->request->isPost()) {
            $_form = input('');
            $ret = $this->archives_service->edit("season",$_form,$_form['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            $id = input('id');
            $ret = $this->archives_service->getOne("season",$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->error($this->getMsg());
            }
            $one = $ret->getData("one");
            $this->assign("one",$one);
            return $this->fetch();
        }
    }

    public function seasondel(){
        $id = input('id');
        $ret = $this->archives_service->del("season",$id);
        if($ret->getCode() == Merchant::$Code_Mer_Error){
            return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
        }
        return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
    }

    public function companylist() {
        $where = input('');
        $ret = $this->archives_service->getList("company",$this->merchant['id'],$where);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("records");
        $this->assign("records",$records);
        $this->assign("where",$where);
        return $this->fetch();
    }

    public function companyadd(){
        if($this->request->isPost()) {
            $_form = input('');
            $_form['mer_id'] = $this->merchant['id'];
            $ret = $this->archives_service->add("company", $_form,$this->merchant['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error){
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            return $this->fetch();
        }
    }

    public function companyedit() {
        if($this->request->isPost()) {
            $_form = input('');
            $ret = $this->archives_service->edit("company",$_form,$_form['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            $id = input('id');
            $ret = $this->archives_service->getOne("company",$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->error($this->getMsg());
            }
            $one = $ret->getData("one");
            $this->assign("one",$one);
            return $this->fetch();
        }
    }

    public function companydel() {
        $id = input('id');
        $ret = $this->archives_service->del("company",$id);
        if($ret->getCode() == Merchant::$Code_Mer_Error){
            return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
        }
        return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
    }

    public function expresslist() {
        $where = input('');
        $ret = $this->archives_service->getList("express",$this->merchant['id'],$where);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("records");
        $this->assign("records",$records);
        $this->assign("where",$where);
        return $this->fetch();
    }

    public function expressadd(){
        if($this->request->isPost()) {
            $_form = input('');
            $_form['mer_id'] = $this->merchant['id'];
            $ret = $this->archives_service->add("express", $_form,$this->merchant['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error){
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            return $this->fetch();
        }
    }

    public function expressedit() {
        if($this->request->isPost()) {
            $_form = input('');
            $ret = $this->archives_service->edit("express",$_form,$_form['id']);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            $id = input('id');
            $ret = $this->archives_service->getOne("express",$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->error($this->getMsg());
            }
            $one = $ret->getData("one");
            $this->assign("one",$one);
            return $this->fetch();
        }
    }

    public function expressdel() {
        $id = input('id');
        $ret = $this->archives_service->del("express",$id);
        if($ret->getCode() == Merchant::$Code_Mer_Error){
            return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
        }
        return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
    }

    public function wxpay () {
        $this->assign('third_menu', []);
        if($this->request->isPost()) {
            $_post = input('');
            $pay_service = new MerPay();
            $ret = $pay_service->getBy($this->merchant['id'],0);
            $ret2 = null;
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                $_post['mer_id'] = $this->merchant['id'];
                $ret2 = $pay_service->insertMerPay($_post);
            }else {
                $one = $ret->getData("one");
                $ret2 = $pay_service->saveMerPay($_post, $one['id']);
            }
            if($ret2->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret2->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret2->getMsg())->setReload()->toJson();
        }else {
            $pay_service = new MerPay();
            $ret = $pay_service->getBy($this->merchant['id'],0);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->fetch();
            }
            $one = $ret->getData("one");
            $this->assign("one",$one);
            return $this->fetch();
        }
    }

    public function alipay () {
        $this->assign('third_menu', []);
        if($this->request->isPost()) {
            $_post = input('');
            $pay_service = new MerPay();
            $ret = $pay_service->getBy($this->merchant['id'],1);
            $ret2 = null;
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                $_post['mer_id'] = $this->merchant['id'];
                $ret2 = $pay_service->insertMerPay($_post);
            }else {
                $one = $ret->getData("one");
                $ret2 = $pay_service->saveMerPay($_post, $one['id']);
            }
            if($ret2->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret2->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret2->getMsg())->setReload()->toJson();

        }else {
            $pay_service = new MerPay();
            $ret = $pay_service->getBy($this->merchant['id'],1);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->fetch();
            }
            $one = $ret->getData("one");
            $this->assign("one",$one);
            return $this->fetch();
        }
    }
}